package com.koron.sinoprobe.web.ledger.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.web.ledger.bean.query.SpPlanMilestoneQuery;
import com.koron.sinoprobe.web.plan.bean.entity.SpPlanMilestone;
import com.koron.sinoprobe.web.plan.bean.vo.SpPlanMilestoneVO;
import com.koron.sinoprobe.web.plan.service.SpPlanMilestoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Comparator;
import java.util.List;

/**
 * @author twcao
 * @title: SpMilestoneController
 * @projectName GDH_EAM_V2
 * @description: 专项台账-里程碑
 * @date 2021/12/28:42
 */
@Slf4j
@RestController
@RequestMapping("/sp/project/milestone")
@Api(tags = "专项台账-里程碑")
public class SpMilestoneController {

    @Autowired
    private SpPlanMilestoneService milestoneService;

    @PostMapping("/update")
    @ApiOperation("修改里程碑，根据id更新实际开始时间、实际结束时间、状态、进度（其他字段更新无效）")
    public Response<SpPlanMilestone> saveOrUpdate(@CasUser @ApiIgnore EamUser user, @RequestBody SpPlanMilestone detail) {
        detail = ADOConnection.runTask(user.getCurrDs(), milestoneService, "updateById", SpPlanMilestone.class, detail);
        return Response.ok("修改成功", detail);
    }

    @GetMapping("/getBySpId")
    @ApiOperation("根据计划/项目id查询里程碑")
    @ApiImplicitParam(name = "spId", value = "计划/项目id", required = true)
    public Response<List<SpPlanMilestoneVO>> getBySpId(@CasUser @ApiIgnore EamUser user, @RequestParam("spId") String spId) {
        List<SpPlanMilestoneVO> list = ADOConnection.runTask(user.getCurrDs(), milestoneService, "getBySpId", List.class, spId);
        return Response.ok("修改成功", list);
    }

    @GetMapping("/queryList")
    @ApiOperation("里程碑条件查询")
    public Response<PageInfo<SpPlanMilestoneVO>> queryList(@CasUser @ApiIgnore EamUser user, SpPlanMilestoneQuery query) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<SpPlanMilestoneVO> list = ADOConnection.runTask(user.getCurrDs(), milestoneService, "queryList", List.class, query);
        return Response.ok("查询完成", new PageInfo<>(list));
    }

    @GetMapping("/queryNameList")
    @ApiOperation("里程碑名字条件查询")
    @ApiImplicitParam(name = "name", value = "里程碑名称")
    public Response<Object> queryNameList(@CasUser @ApiIgnore EamUser user, PageQuery query, String name) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<String> list = ADOConnection.runTask(user.getCurrDs(), milestoneService, "queryNameList", List.class, name);
        return Response.ok("查询完成", new PageInfo<>(list));
    }


    @GetMapping("/getLatestBySpId")
    @ApiOperation("根据计划/项目id查询里程碑")
    @ApiImplicitParam(name = "spId", value = "计划/项目id", required = true)
    public Response<SpPlanMilestoneVO> getLatestBySpId(@CasUser @ApiIgnore EamUser user, @RequestParam("spId") String spId) {
        List<SpPlanMilestoneVO> list = ADOConnection.runTask(user.getCurrDs(), milestoneService, "getBySpId", List.class, spId);
        SpPlanMilestoneVO one = null;
        if (CollectionUtil.isNotEmpty(list)) {
            one = list.stream()
                    .sorted(Comparator.comparingInt(SpPlanMilestone::getSort))
                    .filter(x -> x.getFactEndTime() == null)
                    .findFirst()
                    .orElseGet(() -> list.get(list.size() - 1));
            one.setPercentage((float) Math.round(one.getSort() * 100 / list.size()));
        }
        return Response.ok(one);
    }
}
